package com.raft.framework.base

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProviders
import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup

open class RFBaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (onlyPortrait()) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }

    protected fun <T: ViewModel> provideViewModel(modelClass:Class<T>):T{
        return ViewModelProviders.of(this).get(modelClass)
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        if (autoHackStatusBar()) {
            setTranslucentStatus()
        }
    }

    override fun setContentView(view: View) {
        super.setContentView(view)
        if (autoHackStatusBar()) {
            setTranslucentStatus()
        }
    }

    open protected fun onlyPortrait(): Boolean {
        return true
    }

    open protected fun autoHackStatusBar(): Boolean {
        return true
    }


    protected fun setTranslucentStatus() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            (findViewById<View>(android.R.id.content) as? ViewGroup)?.getChildAt(0)?.fitsSystemWindows = true

        }
    }

}